package com.itheima.web.controller.store;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.store.Product;
import com.itheima.utils.BeanUtil;
import com.itheima.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/store/product")
public class ProductServlet extends BaseServlet {
    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int currentPage = 1;
        int pageSize = 5;
        if (StringUtils.isNotBlank(request.getParameter("currentPage"))) {
            currentPage = Integer.parseInt(request.getParameter("currentPage"));
        }
        if (StringUtils.isNotBlank(request.getParameter("pageSize"))) {
            pageSize = Integer.parseInt(request.getParameter("pageSize"));
        }
        PageInfo all = productService.findAll(currentPage, pageSize);
        request.setAttribute("page", all);
        request.getRequestDispatcher("/pages/store/product/product-list.jsp").forward(request, response);
    }

    public void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/pages/store/product/product-add.jsp").forward(request, response);
    }

    public void save(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Product product = BeanUtil.fillBean(request, Product.class, "yyyy-MM-dd");
        productService.save(product);
        response.sendRedirect(request.getContextPath() + "/store/product?operation=list");
    }

    public void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        Product product = productService.findById(id);
        request.setAttribute("product", product);
        System.out.println("product = " + product);
        request.getRequestDispatcher("/pages/store/product/product-update.jsp").forward(request, response);
    }

    public void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {

        Product product = BeanUtil.fillBean(request, Product.class, "yyyy-MM-dd");
        System.out.println("product = " + product);

        productService.update(product);

        response.sendRedirect(request.getContextPath() + "/store/product?operation=list");
    }

    public void deleteByIds(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String[] ids = request.getParameterValues("ids");
        //System.out.println(ids);
        for (String id : ids) {
            productService.deleteByIds(id.split(","));
        }
        response.sendRedirect(request.getContextPath() + "/store/product?operation=list");
    }

    public void close(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String[] arr = request.getParameterValues("ids");
        productService.close(arr);
        response.sendRedirect(request.getContextPath() + "/store/product?operation=list");
    }

    public void open(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String[] arr = request.getParameterValues("ids");
        productService.open(arr);
        response.sendRedirect(request.getContextPath() + "/store/product?operation=list");
    }

    public void findByName(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String pName = request.getParameter("pName");
        int currentPage = 1;
        int pageSize = 5;
        if (StringUtils.isNotBlank(request.getParameter("currentPage"))) {
            currentPage = Integer.parseInt(request.getParameter("currentPage"));
        }
        if (StringUtils.isNotBlank(request.getParameter("pageSize"))) {
            pageSize = Integer.parseInt(request.getParameter("pageSize"));
        }
        PageInfo all = productService.findByName(pName, currentPage, pageSize);
        request.setAttribute("page", all);
        request.getRequestDispatcher("/pages/store/product/product-list.jsp").forward(request, response);
    }

    public void show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        Product product = productService.findById(id);
        request.setAttribute("product", product);
        request.getRequestDispatcher("/pages/store/product/product-show.jsp").forward(request, response);
    }
}
